/*
 * Stack.h
 *
 *  Created on: 2-dec.-2013
 *      Author: Axel
 */

#ifndef STACK_H_
#define STACK_H_

template <class T>
class Stack {
public:
	Stack();
	~Stack();

	void add(T x);
	T pop();

	int length() {
		return size;
	}

	T& operator[] (const int depth)	{
		return list[depth];
	}

private:
	T* list;
	int size;
	int current;

	//helper function to increase size
	T* increaseSize();
};

template <class T>
Stack<T>::Stack() {
	list = new T[1];
	size = 1;
	current = 0;
}


template <class T>
Stack<T>::~Stack() {
	delete[] list;
}

template <class T>
void Stack<T>::add(T x) {
	if (current == size) {
		list = increaseSize();
	}

	list[current] = x;
	current++;
}


template <class T>
T Stack<T>::pop() {
	current --;
	return list[current+1];
}


template <class T>
T* Stack<T>::increaseSize() {
	const int si = size * 1.5;
	T* newlist = new T[si];

	for(int i=0;i<size;i++)
		newlist[i] = list[i];

	size *= 1.5;
	delete[] list;

	return newlist;
}

#endif /* STACK_H_ */
